package org.dromara.admin.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.admin.domain.bo.HomeHouseBo;
import org.dromara.admin.domain.vo.HomeHouseVo;
import org.dromara.admin.domain.HomeHouse;
import org.dromara.admin.mapper.HomeHouseMapper;
import org.dromara.admin.service.IHomeHouseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 首页房源Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-23
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HomeHouseServiceImpl implements IHomeHouseService {

    private final HomeHouseMapper baseMapper;

    /**
     * 查询首页房源
     *
     * @param id 主键
     * @return 首页房源
     */
    @Override
    public HomeHouseVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询首页房源列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 首页房源分页列表
     */
    @Override
    public TableDataInfo<HomeHouseVo> queryPageList(HomeHouseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HomeHouse> lqw = buildQueryWrapper(bo);
        Page<HomeHouseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的首页房源列表
     *
     * @param bo 查询条件
     * @return 首页房源列表
     */
    @Override
    public List<HomeHouseVo> queryList(HomeHouseBo bo) {
        LambdaQueryWrapper<HomeHouse> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HomeHouse> buildQueryWrapper(HomeHouseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HomeHouse> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HomeHouse::getId);
        lqw.eq(bo.getLessorId() != null, HomeHouse::getLessorId, bo.getLessorId());
        lqw.eq(bo.getSaleId() != null, HomeHouse::getSaleId, bo.getSaleId());
        lqw.eq(StringUtils.isNotBlank(bo.getPropertyCertPhoto()), HomeHouse::getPropertyCertPhoto, bo.getPropertyCertPhoto());
        lqw.eq(StringUtils.isNotBlank(bo.getPropertyNumber()), HomeHouse::getPropertyNumber, bo.getPropertyNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseUrl()), HomeHouse::getHouseUrl, bo.getHouseUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseStatus()), HomeHouse::getHouseStatus, bo.getHouseStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getIsLaunch()), HomeHouse::getIsLaunch, bo.getIsLaunch());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCardPhoto()), HomeHouse::getIdCardPhoto, bo.getIdCardPhoto());
        lqw.like(StringUtils.isNotBlank(bo.getOwnerName()), HomeHouse::getOwnerName, bo.getOwnerName());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCardNumber()), HomeHouse::getIdCardNumber, bo.getIdCardNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), HomeHouse::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), HomeHouse::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getRentalType()), HomeHouse::getRentalType, bo.getRentalType());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), HomeHouse::getAddress, bo.getAddress());
        lqw.like(StringUtils.isNotBlank(bo.getEstateName()), HomeHouse::getEstateName, bo.getEstateName());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseNumber()), HomeHouse::getHouseNumber, bo.getHouseNumber());
        lqw.eq(bo.getFloor() != null, HomeHouse::getFloor, bo.getFloor());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseArea()), HomeHouse::getHouseArea, bo.getHouseArea());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseOrientation()), HomeHouse::getHouseOrientation, bo.getHouseOrientation());
        lqw.eq(bo.getHouseLayoutId() != null, HomeHouse::getHouseLayoutId, bo.getHouseLayoutId());
        lqw.eq(StringUtils.isNotBlank(bo.getElectricalIds()), HomeHouse::getElectricalIds, bo.getElectricalIds());
        lqw.eq(bo.getContractStartDate() != null, HomeHouse::getContractStartDate, bo.getContractStartDate());
        lqw.eq(bo.getContractEndDate() != null, HomeHouse::getContractEndDate, bo.getContractEndDate());
        lqw.eq(StringUtils.isNotBlank(bo.getContractPhoto()), HomeHouse::getContractPhoto, bo.getContractPhoto());
        lqw.eq(StringUtils.isNotBlank(bo.getContractStatus()), HomeHouse::getContractStatus, bo.getContractStatus());
        lqw.eq(bo.getRent() != null, HomeHouse::getRent, bo.getRent());
        return lqw;
    }

    /**
     * 新增首页房源
     *
     * @param bo 首页房源
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HomeHouseBo bo) {
        HomeHouse add = MapstructUtils.convert(bo, HomeHouse.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改首页房源
     *
     * @param bo 首页房源
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HomeHouseBo bo) {
        HomeHouse update = MapstructUtils.convert(bo, HomeHouse.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HomeHouse entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除首页房源信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
